package com.ruoqing.core.exception;

import com.ruoqing.core.constant.Constants;
import com.ruoqing.core.dto.BaseResponse;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

/**
	* @author YaoXian
	* @title: GlobalExceptionHandler
	* @projectName ruoxi-blog
	* @description: 若兮微服务 - 核心模块 - 统一异常处理 - 全局异常处理器
	* @date 2020-11-27 19:55
	*/
@ControllerAdvice
public class GlobalExceptionHandler {
		
		/**
			* 全局异常处理
			* @param e
			* @return BaseResponse
			*/
		@ExceptionHandler(Exception.class)
		@ResponseBody
		public BaseResponse baseErrorHandler(Exception e){
				e.printStackTrace();
				return BaseResponse.buildResponse(Constants.responseError,
								"服务器内部出错了!");
		}
		
		/**
			* 用户服务自定义异常处理
			* @param e
			* @return BaseResponse
			*/
		@ExceptionHandler(UserException.class)
		@ResponseBody
		public BaseResponse userErrorHandler(UserException e){
				e.printStackTrace();
				return BaseResponse.buildResponse(Constants.responseError,
								e.getMsg());
		}
		
		/**
			* 通知服务自定义异常处理
			* @param e
			* @return BaseResponse
			*/
		@ExceptionHandler(SmsException.class)
		@ResponseBody
		public BaseResponse smsErrorHandler(SmsException e){
				e.printStackTrace();
				return BaseResponse.buildResponse(Constants.responseError,
								e.getMsg());
		}
}
